<?php


namespace catchAdmin\diseasepath\controller;


use app\common\model\BaseModel;
use app\common\model\DiseasePatientTask;
use app\common\model\DiseasePatientTaskDetail;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
use MongoDB\BSON\ObjectId;
use think\exception\ValidateException;
use think\model\Collection;

class SolutionCenter
{
    protected $diseasePathModel = null;

    public function __construct()
    {
        $this->diseasePathModel = new DiseasePatientTask();
    }

    public function index(CatchRequest $request): \think\response\Json
    {
        $condition = [];
        $taskStatus = $request->param("task_status", 1, "int");
        $patientName = $request->param("patient_name", "", "htmlspecialchars");
        $condition['planList.plan_task_status'] = ['$eq' => $taskStatus];
        if (trim(!empty($patientName))) {
            $condition["patient_name"] = ['$regex' => $patientName];
        }
        $result = (new DiseasePatientTask())->getLists($request, $condition);
        return json($result);
    }

    public function processingList(CatchRequest $request): \think\response\Json
    {
        $taskId = $request->param("task_id", "", "string");
        if (empty(trim($taskId))) {
            throw new ValidateException("任务id不能为空");
        }
        $patientInfo = (new DiseasePatientTaskDetail())->getPatientByTaskId($taskId);
        $result = (new DiseasePatientTaskDetail())->getProcessList($taskId);

        return CatchResponse::success([
            "patientInfo" => $patientInfo,
            "result" => $result
        ]);
    }

    public function modifyRemark(CatchRequest $request): \think\response\Json
    {
        $taskDetailId = $request->param("detail_id", "", "string");
        if (trim(empty($taskDetailId))) {
            throw new ValidateException("序号不能为空");
        }
        $remark = $request->param("remark", "", "string");
        # 根据detailid修改状态
        $result = (new DiseasePatientTaskDetail())->where("id", "=", new ObjectId($taskDetailId))->save([
            "remark" => $remark
        ]);
        if ($result !== false) {
            (new DiseasePatientTaskDetail())->modifyStatus($taskDetailId, 2, '');
        }
        return CatchResponse::success();

    }


}